<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }
        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
    </style>
    <script type="text/javascript">
        var uid = getQueryVariable("userid");
        var pageNum = getQueryVariable("pageNum");
        var roleid = getQueryVariable("roleid");
        //页面初始化
        $(function () {
            $("input[name=userid]").val(uid);
            //使Ajax同步执行而不是默认的异步执行
            $.ajaxSettings.async = false;
            $('#backid').click(function(){
                window.location.href="/User/index.html?pageNum="+pageNum;
            });
            $.ajax({
                url:"/userdetail/"+uid,
                data:"",
                type: "GET",
                success:function (vo) {
                    console.log(vo);
                    let obj = vo.obj;
                    $("input[name=username]").val(obj.username);
                    $("input[name=password]").val(obj.password);
                    $("input[name=realname]").val(obj.realname);
                    $("input[name=email]").val(obj.email);

                    let statuses = $("input[name=zt]");
                    for (let i = 0; i < statuses.length; i++) {
                        // alert($(sfzfs[i]).val() + ", " + obj.sfzf)
                        if (statuses[i].value == obj.zt) statuses[i].checked = true;
                    }
                    loadRole();
                }
            })
            //异步请求，后端更新数据
            $("#saveInfo").click(function (){
                // alert($("#modifiedForm").serialize());
                alert($("#modifiedForm").serialize() + "&_method=PUT");
                $.ajax({
                    url:"/updateUser",
                    type:"POST",
                    data:$("#modifiedForm").serialize() + "&_method=PUT",
                    dataType:"JSON",
                    success:function (vo) {
                        if (vo.code == 200){
                            alert("用户信息更改成功！");
                            // alert("pageNum:"+$("#currentPageInput").val());
                            window.location.href = "/User/index.html?pageNum=" + pageNum;
                        } else{
                            alert(vo.message);
                        }
                    }
                })
            });
        });
        //从url里获取参数，window.location.search.search从问号(?)开始的 URL（查询部分）
        function getQueryVariable(variable){
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return false;
        }
        //加载角色数据
        function loadRole() {
            $.ajax({
                url:"/rolelist",
                type: 'GET',
                data:"",
                dataType:'JSON',
                async:false,
                success:function (vo){
                    let roles = vo.objList;
                    let options = "";
                    for (let i = 0; i < roles.length; i++) {
                        let role = roles[i];
                        let selected = roleid == role.roleid?"selected=\'selected\'":"";
                        options += "<option value=" + "\'" + role.roleid + "\'" + selected + ">" + role.rolename + "</option>"
                    }
                    $("#roleSelect").html(options);
                }
            })
        }
    </script>
</head>
<body>
<form id="modifiedForm" class="definewidth m20">
<input type="hidden" name="id" value="{$user.id}" />
    <input id="userid" name="userid" type="text" style="display: none">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">登录名</td>
            <td><input name="username"></td>
        </tr>
        <tr>
            <td class="tableleft">密码</td>
            <td><input type="password" name="password"/></td>
        </tr>
        <tr>
            <td class="tableleft">真实姓名</td>
            <td><input type="text" name="realname" value=""/></td>
        </tr>
        <tr>
            <td class="tableleft">邮箱</td>
            <td><input type="text" name="email" value=""/></td>
        </tr>
        <tr>
            <td class="tableleft">状态</td>
            <td>
                <input type="radio" name="zt" value="1"/> 启用
                <input type="radio" name="zt" value="0"/> 禁用
            </td>
        </tr>
        <tr>
            <td class="tableleft">角色</td>
            <td>
            	<select name="roleid" id="roleSelect">
       			 </select>
        	</td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button id="saveInfo" class="btn btn-primary" type="button">更新</button>&nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>
    $(function () {       
		$('#backid').click(function(){
				window.location.href="/User/index.html?pageNum="+pageNum;
		 });
    });
</script>